package com.thingsgrid.device.controller;

import com.thingsgrid.appmanager.feign.FileFeign;
import com.thingsgrid.device.service.TopmService;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
@AllArgsConstructor
public class TopmController {

	private TopmService service;
	/**
	 * 调用文件上传模块
	 */
	private FileFeign fileFeign;
	/**
	 * 获得所有产品类型
	 */
	@PostMapping("/topm/productType")
	public R<List> GetProductTypeList() {
		List list = service.GetProductTypeList();
		return R.data(list);
	}
	/**
	 * 获得某一产品类型的产品名列表
	 */
	@PostMapping("/topm/productName")
	public R<List> GetProductName(String productTypeId) {
		List productNamelist = service.GetProductNameList(productTypeId);
		return R.data(productNamelist);
	}
	/**
	 * 获得某一产品的设备名列表
	 */
	@PostMapping("/topm/deviceName")
	public R<List> GetDeviceName(String productName) {//刷新返回类型的产品名的map
		List productTypelist = service.GetDeviceNameList(productName);
		return R.data(productTypelist);
	}


	/**
	 * 根据查询条件获得拓扑图信息
	 */
	@PostMapping("/topm")
	public R<List<Map>> QueryTopmByConsition(String productTypeId, String productName, String deviceName){
		return R.data(service.QueryTopmByConsition(productTypeId, productName, deviceName));
	}
	/**
	 * 获得所有产品名
	 */
	@PostMapping("/topm/custom/productName")
	public R<List> GetAllProductName() {
		List productNamelist = service.GetAllProductNameList();
		return R.data(productNamelist);
	}
	/**
	 * 获得所有图标
	 */
	@PostMapping("/topm/icon")
	public R<List> GetIconList() {
		return R.data(service.GetIconList());
	}
	/**
	 * 获得产品对应的图标及默认图标
	 */
	@PostMapping("/topm/icon/deviceIcon")
	public R<Map> QueryDeviceIconList(String productName){
		return R.data(service.QueryDeviceIconList(productName));
	}
	/**
	 * 新增图标，并保存
	 */
	@PostMapping("/topm/icon/save")
	public R SaveIcon(@RequestPart("file") MultipartFile[] fileList) {
		List<String> iconAddressList = new ArrayList<>();
		for (MultipartFile file : fileList){
			try {
				String iconStress = fileFeign.uploadFile(file);
				iconAddressList.add("image:\\"+iconStress);
			}catch (Exception e){
				e.printStackTrace();
				return R.status(false);
			}
		}
		if(service.IconSave(iconAddressList)){
			return R.data("上传成功");
		}else {
			return R.data("上传成功,保存失败");
		}
	}
	/**
	 * 更新产品所使用的图标
	 */
	@PostMapping("/topm/icon/update")
	public R UpdataIcon(@RequestParam String iconId, String productName) {
		int resultInt = service.IconUpdata(iconId, productName);
		if ( resultInt == 1){
			return R.data("设置图标为默认图标");
		}else if ( resultInt == 2){
			return R.data("设置图标已用于其他产品");
		}else if ( resultInt == 3){
			return R.data("更新失败");
		}else {
			return R.data("更新成功");
		}
	}
	/**
	 * 获得产品对应的链路及默认链路
	 */
	@PostMapping("/topm/deviceLink")
	public R<Map> QueryDeviceLinkList(@RequestParam String productName) {
		return R.data(service. QueryDeviceLinkList(productName));
	}
	/**
	 * 更新产品链路
	 */
	@PostMapping("/topm/link/update")
	public R UpdataIcon(@RequestParam Integer linkStrong, String productName, String linkColor) {
		return R.status(service.LinkUpdata(linkStrong, productName, linkColor));
	}
}
